<template>
  <div class="housing">
    <h4>房源管理</h4>

    <!-- @tab-click="handleClick" -->
    <el-tabs v-model="activeName">
      <el-tab-pane label="销售楼盘" name="first">
        <div class="search">
          <el-select
            v-model="province"
            placeholder="请选择省份"
            @change="selectprovince"
          >
            <el-option
              v-for="item in provincelist"
              :key="item.id"
              :label="item.province"
              :value="item.provinceid"
            >
            </el-option>
          </el-select>
          <el-select v-model="city" placeholder="请选择市" @change="selectcity">
            <el-option
              v-for="item in citylist"
              :key="item.cityid"
              :label="item.city"
              :value="item.cityid"
            >
            </el-option>
          </el-select>
          <el-select
            v-model="area"
            placeholder="请选择区县"
            @change="selectarea"
          >
            <el-option
              v-for="(item,index) in arealist"
              :key="index"
              :label="item.area"
              :value="item.area"
            >
            </el-option>
          </el-select>
          <p>
            <input type="text" placeholder="关键字" v-model="searchs" /><button
              @click="btnsou"
            >
              搜索
            </button>
          </p>
        </div>
        <button class="btn" @click="btnadd">新增</button>
        <Add ref="dialog" @update="pagelist()" />

        <el-table :data="tableData" border style="width: 100%">
          <el-table-column fixed type="index" label="序号" width="200">
          </el-table-column>

          <el-table-column prop="buildName" label="楼盘名称" width="200">
          </el-table-column>

          <el-table-column prop="address" label="所属区域" width="200">
          </el-table-column>

          <el-table-column prop="price" label="价格" width="200">
          </el-table-column>

          <el-table-column prop="time" label="更新时间" width="200">
          </el-table-column>

          <el-table-column prop="sort" label="排序" width="200">
          </el-table-column>

          <el-table-column prop="state" label="状态" width="200">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="300">
            <template slot-scope="scope">
              <el-button type="text" size="small">户型</el-button>
              <el-divider direction="vertical"></el-divider>
              <el-button type="text" size="small">相册</el-button>
              <el-divider direction="vertical"></el-divider>
              <el-button type="text" size="small">动态</el-button>
              <el-divider direction="vertical"></el-divider>
              <el-button type="text" size="small">沙盘</el-button>
              <el-divider direction="vertical"></el-divider>
              <el-button type="text" size="small" @click="edit(scope.row)"
                >编辑</el-button
              >
              <el-divider direction="vertical"></el-divider>
              <el-button type="text" size="small" @click="del(scope)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="房屋租赁" name="second">
        <div class="searchc">
          <p>
            <input type="text" placeholder="关键字" v-model="searchs" /><button
              @click="btnsou"
            >
              搜索
            </button>
          </p>
        </div>
        <button class="btn" @click="btnaddd">新增</button>
        <Addd ref="dialog2" @update="pagelis()" />

        <el-table :data="table" border style="width: 100%">
          <el-table-column fixed type="index" label="序号" width="200">
          </el-table-column>

          <el-table-column prop="Villagename" label="小区名称" width="200">
          </el-table-column>

          <el-table-column prop="title" label="标题" width="200">
          </el-table-column>

          <el-table-column prop="Size" label="面积" width="200">
          </el-table-column>

          <el-table-column prop="price" label="总价" width="200">
          </el-table-column>

          <el-table-column prop="sort" label="排序" width="200">
          </el-table-column>

          <el-table-column prop="state" label="状态" width="200">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="300">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="editt(scope.row)"
                >编辑</el-button
              >
              <el-divider direction="vertical"></el-divider>
              <el-button type="text" size="small" @click="del(scope)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageNum"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import Add from "../../components/add.vue";
import Addd from "../../components/addd.vue";
export default {
  data() {
    return {
      tableData: [],
      table: [],
      value: [],
      searchs: "",
      total: 0,
      activeName: "first",
      pageIndex: 1,
      pageNum: 5,
      currentPage4: 1,
      province: "",
      city: "",
      area: "",
      provinceid: "",
      cityid: "",
      areaid: "",
      provincelist: [],
      citylist: [],
      arealist: [],
    };
  },

  components: {
    Add,
    Addd,
  },

  methods: {
    //销售楼盘分页
    pagelist(pageIndex, pageNum) {
      this.$http
        .post("/api/changepage", {
          pageIndex: pageIndex,
          pageNum: pageNum,
          state: 1,
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.data;
          this.total = res.data.total;
        });
    },
    //房屋租赁分页
    pagelis(pageIndex, pageNum) {
      this.$http
        .post("/api/changepage", {
          pageIndex: pageIndex,
          pageNum: pageNum,
          state: 0,
        })
        .then((res) => {
          console.log(res);
          this.table = res.data.data;
          this.total = res.data.total;
        });
    },

    handleSizeChange(val) {
      this.pageNum = val;
      this.pagelist(this.pageIndex, this.pageNum);
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.pagelist(this.pageIndex, this.pageNum);
    },

    //销售楼盘添加
    btnadd() {
      this.$refs.dialog.dialogFormVisible = true;
      this.$refs.dialog.form = {
        buildName: "",
        address: "",
        price: "",
        time: "",
        sort: "",
        state: "",
      };
    },
    //房屋租赁添加
    btnaddd() {
      this.$refs.dialog2.dialogFormVisible = true;
      this.$refs.dialog2.form = {
        Villagename: "",
        title: "",
        Size: "",
        price: "",
        sort: "",
        state: "",
      };
    },

    //销售楼盘编辑
    edit(editt) {
      this.$refs.dialog.dialogFormVisible = true;
      this.$refs.dialog.form = JSON.parse(JSON.stringify(editt));
    },
    //房屋租赁编辑
    editt(edittt) {
      this.$refs.dialog2.dialogFormVisible = true;
      this.$refs.dialog2.form = JSON.parse(JSON.stringify(edittt));
    },

    //删除
    del(scope) {
      console.log(scope.row.id);
      this.$http.post("/api/deleteAdd", { id: scope.row.id }).then((res) => {
        console.log(res);
        if (res.data.code === 1) {
          this.$message.success("删除成功");
          this.pagelist(this.pageIndex, this.pageNum);
        }
      });
    },

    //搜索
    btnsou() {
      this.$http
        .post("/api/search", {
          area: this.searchs,
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.data;
        });
    },
    //获取省
    getsheng() {
      this.$http.get("/api/sheng").then((res) => {
        console.log(res);
        if (res.data.code === 1) {
          this.provincelist = res.data.data;
        }
      });
    },
    //改变三级联动省 获取市
    selectprovince(value) {
      console.log(value);
      this.provinceid = value;
      this.$http
        .post("/api/shi", { provinceid: this.provinceid })
        .then((res) => {
          console.log(res);
          if (res.data.code === 1) {
            this.citylist = res.data.data;
          }
        });
    },
    //获取县
    selectcity(value) {
      console.log(value);
      this.cityid = value;
      this.$http.post("/api/xian", { cityid: this.cityid }).then((res) => {
        console.log(res);
        if (res.data.code === 1) {
          this.arealist = res.data.data;
        }
      });
    },
    //三级联动区县
    selectarea() {
      // this.$http.post("/api/xian").then((res) => {
      //   console.log(res);
      // });
    },
  },
  created() {
    //销售楼盘
    this.pagelist(this.pageIndex, this.pageNum);
    //房屋租赁
    this.pagelis(this.pageIndex, this.pageNum);
    //省
    this.getsheng();
  },
};
</script>

<style>
h4 {
  width: 100%;
  height: 50px;
  line-height: 50px;
}
.el-tabs {
  background: #fff;
}
.housing {
  background: #e6e6e6;
}
.housing-one {
  width: 100%;
  height: 50px;
  display: flex;
}
.cell {
  text-align: center;
}
.btn {
  padding: 5px 15px;
  border: none;
  background: skyblue;
  color: #fff;
}
.el-pagination {
  text-align: center;
}
.el-select:nth-child(2) {
  padding: 0 10px;
}
.search {
  display: flex;
  height: 50px;
  display: flex;
  align-items: center;
}
.search p {
  padding: 0 10px;
}
.search p input {
  padding: 10px 5px;
  border-radius: 5px;
  border: 1px solid #e6e6e6;
}
.search p button {
  margin: 0 5px;
  padding: 9px 15px;
  background: skyblue;
  border: none;
  color: #fff;
}
.searchc {
  display: flex;
  height: 50px;
  display: flex;
  align-items: center;
}
.searchc p {
  padding: 0 10px;
}
.searchc p input {
  padding: 10px 5px;
  border-radius: 5px;
  border: 1px solid #e6e6e6;
}
.searchc p button {
  margin: 0 5px;
  padding: 9px 15px;
  background: skyblue;
  border: none;
  color: #fff;
}
</style>